package com.doctor.ui.medicalknowledge;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.doctor.adapter.KnowledgeSecAndThirdAdapter;
import com.doctor.adapter.KnowledgeThirdAdapter.ThirdClickCallback;
import com.doctor.bean.KnowledgeBean;
import com.doctor.comm.ConstConfig;
import com.doctor.comm.FragmentHelper;
import com.doctor.comm.LoadingTip;
import com.doctor.comm.MyApplication;
import com.doctor.database.DbOperator;
import com.doctor.ui.BaseFragment;
import com.doctor.ui.R;

public class KnowledgeSecondFrag extends BaseFragment{

	/**
	 * 医学知识医疗技术二三级目录
	 */
	
	private Context context;
	private ListView listView;
	private KnowledgeSecAndThirdAdapter adapter;
	private KnowledgeBean firstBean;
	private TextView firstName;
	private KnowledgeFourthFrag fourthFrag;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		context=getActivity();
		firstBean=(KnowledgeBean) getArguments().getSerializable(ConstConfig.BEAN);
		View view=inflater.inflate(R.layout.knowledge_sec_and_thir, container, false);
		initiaView(view);
		getData();
		
		return view;
	}
	
	@Override
	protected void initiaView(View view) {
		// TODO Auto-generated method stub
		firstName=(TextView) view.findViewById(R.id.firstDir);
		firstName.setText(firstBean.getDirname());
		listView=(ListView) view.findViewById(R.id.listView);
	}
	
	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
		context=null;
		listView=null;
		adapter=null;
		firstBean=null;
		firstName=null;
		fourthFrag=null;
	}
	
	@Override
	protected void getData() {
		// TODO Auto-generated method stub
		LoadingTip.showDialog(context, getString(R.string.loading));
		new Thread() {
			public void run() {
				final List<List<KnowledgeBean>> all = DbOperator.getInstance(context).selectDirs(firstBean.getId());
				listView.postDelayed(createUIThreadRunnable(all), DbOperator.delayTime);
			}
		}.start();
	}
	
	
	/**
	 * 生成一个在UI主线程运行的Runnable
	 * @param all 二三级栏目列表
	 * @return
	 */
	private Runnable createUIThreadRunnable(final List<List<KnowledgeBean>> all) {
		Runnable mRunnable = new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if (all != null && all.size() > 0) {
					adapter=new KnowledgeSecAndThirdAdapter(context, all, new ThirdClickCallback() {
						@Override
						public void onClick(KnowledgeBean bean) {
							// TODO Auto-generated method stub
							fourthFrag=null;
							fourthFrag=new KnowledgeFourthFrag();
							FragmentTransaction transaction=getActivity().getSupportFragmentManager().beginTransaction();
							Bundle bundle=new Bundle();
							bundle.putSerializable(ConstConfig.BEAN, bean);
							fourthFrag.setArguments(bundle);
							FragmentHelper.setFragSwitchAnimation(transaction);
							transaction.replace(R.id.frag_container, fourthFrag).addToBackStack(null).commit();
							
							//进入下一级栏目后，如果当前栏目是有new标志的则将该标志清除
							if (bean.getIsNew()) {
								ContentValues values = new ContentValues();
								values.put("_is_new", 0);
								int result = DbOperator.getInstance(MyApplication.getInstance()).updateData(ConstConfig.CATEGORT_TABLE, values, bean.getId());
								if (result > 0) {
									bean.setIsNew(false);
									adapter.notifyDataSetChanged();
								}
							}
						}
					});
					listView.setAdapter(adapter);
				}
			LoadingTip.hideDialog();
			}
		};
		
		return mRunnable;
	}
	
	
	
}
